package com.examination.mlib.view;

import static android.graphics.Paint.ANTI_ALIAS_FLAG;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;

import com.examination.mlib.R;
import com.yilijk.base.utils.ScreenUtils;


public class TrapezoidTextView extends androidx.appcompat.widget.AppCompatTextView {
    private static final int TILT_WIDTH = 10;
    private Context context;
    private boolean isLeft = true, isNoTrapezoid = false;
    private int tiltWidth = TILT_WIDTH;
    private Path linePath;
    private Paint clearPaint;
    private int width, height;

    public TrapezoidTextView(Context context) {
        super(context);
        init(context, null);
    }

    public TrapezoidTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TrapezoidTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public void init(Context context, AttributeSet attrs) {
        this.context = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TrapezoidTextView);
        isLeft = typedArray.getBoolean(R.styleable.TrapezoidTextView_isLeft, true);
        isNoTrapezoid = typedArray.getBoolean(R.styleable.TrapezoidTextView_isNoTrapezoid, false);
        tiltWidth = typedArray.getInteger(R.styleable.TrapezoidTextView_tiltWidth, TILT_WIDTH);
        typedArray.recycle();

        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        linePath = new Path();
        clearPaint = new Paint(ANTI_ALIAS_FLAG);
        clearPaint.setStyle(Paint.Style.FILL);
        clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
//        clearPaint.setColor(getResources().getColor(R.color.transparent));
//        clearPaint.setColor(context.getResources().getColor(R.color.them));


    }

    public void setNoTrapezoid(boolean noTrapezoid) {
        isNoTrapezoid = noTrapezoid;
        postInvalidate();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width = getWidth();
        height = getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isNoTrapezoid) {
            return;
        }
        if (isLeft) {
//            setBackground(getResources().getDrawable(R.color.transparent));
            linePath.moveTo(width, 0);
            linePath.lineTo(width - ScreenUtils.dip2px(context, tiltWidth), height);
            linePath.lineTo(width, height);
            linePath.close();
            canvas.drawPath(linePath, clearPaint);
        } else {
            linePath.moveTo(0, 0);
            linePath.lineTo(ScreenUtils.dip2px(context, tiltWidth), 0);
            linePath.lineTo(0, height);
            linePath.close();
            canvas.drawPath(linePath, clearPaint);
        }
    }
}
